package com.scs.application.modules.base.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.query.QueryContext;
import com.scs.application.core.query.QueryContextHolder;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.base.dto.OprtDTO;
import com.scs.application.modules.base.entity.Oprt;
import com.scs.application.modules.base.service.OprtService;
import com.scs.application.modules.sys.model.PageResult;
import com.scs.application.modules.sys.model.RestResult;
import com.google.common.collect.Maps;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 */
@RestController
@RequestMapping("/base/oprt")
public class OprtController extends CrudController<OprtService, Oprt>{

    @GetMapping("/oprt_list")
    public RestResult oprtList(){
        List<Oprt> oprtList = service.oprtList();
        return RestResult.ok("ok",oprtList);
    }

    /**
     *  根据手术单号获取手术单信息
     * @param code
     * @return
     */
    @GetMapping("/get_oprt_by_code")
    public RestResult getOprtByCode(@RequestParam("code") String code){
        if (StringUtils.isBlank(code)){
            return RestResult.error("error");
        }
        OprtDTO oprt = service.getOprtByCode(code);
        return RestResult.ok("ok", oprt);
    }

    /**
     *  根据手术单号获取手术单和病人信息
     * @param id
     * @return
     */
    @GetMapping("/getOprtById")
    public RestResult getOprtById(@RequestParam("id") Serializable id){
        OprtDTO oprt = service.getOprtById(id);
        return RestResult.ok("查询成功", oprt);
    }

    /**
     * 选择 获取数据列表
     */
    @GetMapping("/pageForSelect")
    public PageResult pageForSelect(@RequestParam Map<String, Object> reqData) {
        QueryContext queryContext = QueryContextHolder.currentContext();
        Map<String, Object> userParam =null;
        //固定参数
        if(reqData.containsKey("userParam")){
            JSONObject ob = JSON.parseObject(reqData.get("userParam")+"");
            userParam= Maps.newHashMap(ob);
        }


        IPage pageResult = service.pageForSelect(queryContext,userParam);
        return PageResult.ok("查询成功", pageResult.getRecords(), pageResult.getTotal());
    }

}
